package com.kucai.yifaqapp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.fragment.DaggerInsertMoneyFragmentComponet;
import com.kucai.yifaqapp.dagger.module.fragment.InsertMoneyFragmentModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.Pay_CzListBean;
import com.kucai.yifaqapp.presenter.fragment.InsertMoneyFragmentPresenter;
import com.kucai.yifaqapp.ui.activity.Z_alipayctivity;
import com.kucai.yifaqapp.ui.activity.Z_bankTransferctivity;
import com.kucai.yifaqapp.ui.activity.Z_cftxinctivity;
import com.kucai.yifaqapp.ui.activity.Z_weixinctivity;
import com.kucai.yifaqapp.ui.activity.Z_ysfctivity;
import com.kucai.yifaqapp.ui.activity.onlinePay;
import com.kucai.yifaqapp.ui.view.InsertMoneyFragmentView;
import com.kucai.yifaqapp.util.EventBus.refreshFragmentEvent;
import com.kucai.yifaqapp.util.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * 存款
 */
public class InsertMoneyFragment extends BaseFragment<InsertMoneyFragmentPresenter> implements InsertMoneyFragmentView {
    @BindView(R.id.insertmoney_rl)
    RecyclerView insertmoneyRl;
    private MyAdapter activtyAdapter;
    private Pay_CzListBean mDatas;

    @Override
    protected void ChangeSkin() {

    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_insertmoney;
    }

    @Override
    protected void initDagger() {
        DaggerInsertMoneyFragmentComponet.builder()
                .insertMoneyFragmentModule(new InsertMoneyFragmentModule(this))
                .build().inject(this);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(InsertMoneyFragment.this);
        }
        InitData();
        InitView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) //在ui线程执行
    public void Event(refreshFragmentEvent messageEvent) {
        //开始网络请求
        InitData();
    }

    private void InitView() {
        insertmoneyRl.setLayoutManager(new LinearLayoutManager(getContext()));
        activtyAdapter = new MyAdapter();
    }

    private void InitData() {
        if (Info.user != null) {
            mPresenter.getCzList(Info.user.token);
        }
    }

    @Override
    public void onGetCzList(Pay_CzListBean resLogin) {
        mDatas = resLogin;
        if (mDatas != null) {
            if (mDatas.getRech_types() != null) {
                insertmoneyRl.setAdapter(activtyAdapter);
            }
        }
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.insert_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            try {
                holder.rukuantype.setText(mDatas.getRech_types().get(position).getName() + "");
                switch (mDatas.getRech_types().get(position).getPayCode()) {
                    case "alipay":
                        holder.img.setBackgroundResource(R.mipmap.pay_alipay);
                        break;
                    case "alipaySm":
                        holder.img.setBackgroundResource(R.mipmap.pay_alipay);
                        break;
                    case "bankTransfer":
                        holder.img.setBackgroundResource(R.mipmap.pay_banktransfer);
                        break;
                    case "BD":
                        holder.img.setBackgroundResource(R.mipmap.pay_bdd);
                        break;
                    case "cft":
                        holder.img.setBackgroundResource(R.mipmap.pay_cft);
                        break;
                    case "JD":
                        holder.img.setBackgroundResource(R.mipmap.pay_jd);
                        break;
                    case "onlinePayment":
                        holder.img.setBackgroundResource(R.mipmap.pay_onlinepayment);
                        break;
                    case "QQ":
                        holder.img.setBackgroundResource(R.mipmap.pay_qq);
                        break;
                    case "weixin":
                        holder.img.setBackgroundResource(R.mipmap.pay_weixin);
                        break;
                    case "WX":
                        holder.img.setBackgroundResource(R.mipmap.pay_wx);
                        break;
                    case "WY":
                        holder.img.setBackgroundResource(R.mipmap.pay_wy);
                        break;
                    case "XYK":
                        holder.img.setBackgroundResource(R.mipmap.pay_xyk);
                        break;
                    case "YL":
                        holder.img.setBackgroundResource(R.mipmap.pay_yl);
                        break;
                    case "YLONLINE":
                        holder.img.setBackgroundResource(R.mipmap.pay_ylonline);
                        break;
                    case "ZFB":
                        holder.img.setBackgroundResource(R.mipmap.pay_zfb);
                        break;
                    case "ysf":
                        holder.img.setBackgroundResource(R.mipmap.ysf);
                        break;
                        default:
                }
                if (mDatas.getRech_types().get(position).getRemark() != null) {
                    holder.textcontent.setText(mDatas.getRech_types().get(position).getRemark() + "");
                } else {
                    holder.textcontent.setText(mDatas.getRech_types().get(position).getName() + "");
                }
                holder.item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPresenter.getCzList(Info.user.token);
//                         WY********网银在线支付
//                         YLONLINE********银联快捷
//                         bankTransfer********银行卡转账
//                         ZFB********支付宝在线支付
//                         WX********微信在线支付
//                         YL********银联扫码
//                         alipay********支付宝转账
//                         weixin********微信转账
//                         cft********财付通转账
//                         JD********京东钱包
                        switch (mDatas.getRech_types().get(position).getPayCode()) {
                            case "alipay":
                                //转账
                                startActivity(new Intent(getContext(), Z_alipayctivity.class));
                                break;
                            case "bankTransfer":
                                //转账
                                startActivity(new Intent(getContext(), Z_bankTransferctivity.class));
                                break;
                            case "BD":
                                Intent BD = new Intent(getActivity(), onlinePay.class);
                                BD.putExtra("PayType", "BD");
                                startActivity(BD);
                                break;
                            case "cft":
                                //转账
                                startActivity(new Intent(getContext(), Z_cftxinctivity.class));
                                break;
                            case "JD":
                                Intent JD = new Intent(getActivity(), onlinePay.class);
                                JD.putExtra("PayType", "JD");
                                startActivity(JD);
                                break;
                            case "onlinePayment":
                                Intent onlinePayment = new Intent(getActivity(), onlinePay.class);
                                onlinePayment.putExtra("PayType", "onlinePayment");
                                startActivity(onlinePayment);
                                break;
                            case "QQ":
                                Intent QQ = new Intent(getActivity(), onlinePay.class);
                                QQ.putExtra("PayType", "QQ");
                                startActivity(QQ);
                                break;
                            case "weixin":
                                //转账
                                startActivity(new Intent(getContext(), Z_weixinctivity.class));
                                break;
                            case "WX":
                                Intent WX = new Intent(getActivity(), onlinePay.class);
                                WX.putExtra("PayType", "WX");
                                startActivity(WX);
                                break;
                            case "WY":
                                Intent WY = new Intent(getActivity(), onlinePay.class);
                                WY.putExtra("PayType", "WY");
                                startActivity(WY);
                                break;
                            case "alipaySm":
                                Intent alipaySm = new Intent(getActivity(), onlinePay.class);
                                alipaySm.putExtra("PayType", "alipaySm");
                                startActivity(alipaySm);
                                break;
                            case "XYK":
                                Intent XYK = new Intent(getActivity(), onlinePay.class);
                                XYK.putExtra("PayType", "XYK");
                                startActivity(XYK);
                                break;
                            case "YL":
                                Intent YL = new Intent(getActivity(), onlinePay.class);
                                YL.putExtra("PayType", "YL");
                                startActivity(YL);
                                break;
                            case "YLONLINE":
                                Intent YLONLINE = new Intent(getActivity(), onlinePay.class);
                                YLONLINE.putExtra("PayType", "YLONLINE");
                                startActivity(YLONLINE);
                                break;
                            case "ZFB":
                                Intent ZFB = new Intent(getActivity(), onlinePay.class);
                                ZFB.putExtra("PayType", "ZFB");
                                startActivity(ZFB);
                                break;
                            case "ysf"://原生的转账
                                Intent ysf = new Intent(getActivity(), Z_ysfctivity.class);
                                ysf.putExtra("PayType", "ysf");
                                startActivity(ysf);
                                break;
                                default:
                        }
                    }
                });
            } catch (Exception e) {

            }
        }

        @Override
        public int getItemCount() {
            if (mDatas.getRech_types() == null) {
                return 0;
            } else {
                return mDatas.getRech_types().size();
            }
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            LinearLayout item;
            TextView rukuantype;
            TextView textcontent;
            ImageView img;

            MViewHolder(View itemView) {
                super(itemView);
                item = (LinearLayout) itemView.findViewById(R.id.item);
                rukuantype = itemView.findViewById(R.id.rukuantype);
                textcontent = itemView.findViewById(R.id.textcontent);
                img = itemView.findViewById(R.id.img);
            }
        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            //刷新操作
            if (Info.user != null) {
                mPresenter.getCzList(Info.user.token);
            }
        }
    }
}
